/*
 * NetworkServer.h
 *
 *  Created on: Apr 12, 2011
 *      Author: Alan Mattila
 */

#ifndef NETWORKSERVER_H_
#define NETWORKSERVER_H_

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>

#include "Packet.h"
#include "Network.h"
#include "../checkers/Checkers.h"

using std::string;

class NetworkServer : public Network
{
	Q_OBJECT

public:
	// Constructor/destructor functions
	NetworkServer();
	NetworkServer(Checkers *_checkers);
	~NetworkServer();

	// Mutator functions
	void SetCheckers(Checkers *_checkers) { checkers = _checkers; }

	// Accessor Functions
	QTcpSocket* GetTcpSocket(void) const { return tcpSocket; }

	int StartHosting();
	int MakeMove();

public slots:
	void Error(void);
	void Disconnected(void);
	void BytesWritten(void);
	int GetPacketFromClient(void);

signals:
	void moveReceived(int, int, int, int);

private:
	Checkers *checkers;

	// Qt variables
	QTcpSocket *tcpSocket;
	QTcpServer *tcpServer;
	quint8 blockSize;

	void CreateMessagePacket(packetMessage_t packetMessage, QByteArray &block);
	int CreateGameDataPacket(QByteArray &block);
	void ParseMove(int x1, int y1, int x2, int y2);
	void SendIllegalMove(void);
	int ParsePacket(void);
};

#endif /* NETWORKSERVER_H_ */
